<!DOCTYPE html>
<html>
<head>
    <title>医疗智能问答系统</title>
    <meta charset="UTF-8">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4CAF50;
            --secondary-color: #E8F5E9;
            --text-color: #2C3E50;
            --border-radius: 12px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            background: url('https://img.freepik.com/free-photo/medical-banner-with-doctor-wearing-coat_23-2149611193.jpg') no-repeat center center fixed;
            background-size: cover;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            color: var(--text-color);
        }

        .header {
            width: 100%;
            background: linear-gradient(135deg, var(--primary-color), #2196F3);
            padding: 15px 0;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            backdrop-filter: blur(5px);
        }

        h1 {
            color: white;
            font-size: 2.2em;
            font-weight: 700;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        }

        .main-container {
            width: 70%;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .chat-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: var(--border-radius);
            box-shadow: 0 8px 30px rgba(0,0,0,0.12);
            overflow: hidden;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255,255,255,0.3);
            margin-top: 20px;
        }

        .chat-box {
            height: 500px;
            overflow-y: auto;
            padding: 20px;
            background: rgba(255, 255, 255, 0.9);
        }

        .message {
            margin: 12px 0;
            padding: 12px 16px;
            border-radius: 12px;
            max-width: 70%;
            position: relative;
            animation: fadeIn 0.3s ease-in-out;
            word-wrap: break-word;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .user-message {
            background: var(--primary-color);
            color: white;
            margin-left: auto;
            border-bottom-right-radius: 5px;
        }

        .bot-message {
            background: var(--secondary-color);
            color: var(--text-color);
            margin-right: auto;
            border-bottom-left-radius: 5px;
        }

        .input-container {
            display: flex;
            padding: 15px;
            background: rgba(255, 255, 255, 0.95);
            border-top: 1px solid rgba(0,0,0,0.1);
            gap: 10px;
        }

        #questionInput {
            flex: 1;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: var(--border-radius);
            font-size: 15px;
            transition: all 0.3s ease;
            font-family: inherit;
        }

        #questionInput:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(76,175,80,0.2);
        }

        button {
            padding: 12px 25px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: var(--border-radius);
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            font-family: inherit;
            min-width: 80px;
        }

        button:hover {
            background: #43A047;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(76,175,80,0.3);
        }

        button:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        /* 自定义滚动条 */
        .chat-box::-webkit-scrollbar {
            width: 8px;
        }

        .chat-box::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 4px;
        }

        .chat-box::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 4px;
        }

        .chat-box::-webkit-scrollbar-thumb:hover {
            background: #666;
        }

        /* 打字机效果的光标 */
        .typing::after {
            content: '|';
            animation: blink 1s infinite;
        }

        @keyframes blink {
            0%, 100% { opacity: 1; }
            50% { opacity: 0; }
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .main-container {
                width: 90%;
                padding: 10px;
            }

            .chat-box {
                height: 400px;
            }

            .message {
                max-width: 85%;
            }
        }

        /* 添加背景遮罩效果 */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.3);
            backdrop-filter: blur(3px);
            z-index: -1;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>医疗智能问答系统</h1>
    </div>
    <div class="main-container">
        <div class="chat-container">
            <div class="chat-box" id="chatBox"></div>
            <div class="input-container">
                <input type="text" id="questionInput" placeholder="请输入您的医疗相关问题...">
                <button onclick="askQuestion()">发送</button>
            </div>
        </div>
    </div>

    <script>
        function addMessage(message, isUser) {
            const chatBox = document.getElementById('chatBox');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
            
            if (isUser) {
                messageDiv.textContent = message;
                chatBox.appendChild(messageDiv);
                chatBox.scrollTop = chatBox.scrollHeight;
                return;
            }
            
            messageDiv.classList.add('typing');
            chatBox.appendChild(messageDiv);
            let index = 0;
            const speed = 50;
            
            function typeWriter() {
                if (index < message.length) {
                    messageDiv.textContent = message.substring(0, index + 1);
                    index++;
                    chatBox.scrollTop = chatBox.scrollHeight;
                    setTimeout(typeWriter, speed);
                } else {
                    messageDiv.classList.remove('typing');
                }
            }
            
            typeWriter();
        }

        function askQuestion() {
            const input = document.getElementById('questionInput');
            const question = input.value.trim();
            const sendButton = document.querySelector('button');
            
            if (question) {
                input.disabled = true;
                sendButton.disabled = true;
                sendButton.textContent = '发送中...';
                
                addMessage(question, true);
                input.value = '';

                fetch('/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `question=${encodeURIComponent(question)}`
                })
                .then(response => response.json())
                .then(data => {
                    addMessage(data.answer, false);
                })
                .catch(error => {
                    addMessage('抱歉，系统出现错误，请稍后重试。', false);
                })
                .finally(() => {
                    input.disabled = false;
                    sendButton.disabled = false;
                    sendButton.textContent = '发送';
                    input.focus();
                });
            }
        }

        document.getElementById('questionInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && !this.disabled) {
                askQuestion();
            }
        });

        window.onload = function() {
            addMessage('您好，我是小李医药智能助理，请问有什么可以帮您？', false);
        };
    </script>
</body>
</html> 